package com.dchh.android.suverynew.act;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.Toolbar;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dchh.android.suverynew.R;
import com.dchh.android.suverynew.base.SBaseActivity;
import com.dchh.android.suverynew.common.SPreferencesManager;
import com.dchh.android.suverynew.presenter.IWelcomePresenter;
import com.dchh.android.suverynew.presenter.impl.WelcomePresenter;
import com.dchh.android.suverynew.view.IWelcomeView;
import com.dhcc.android.base.base.MSApplication;
import com.dhcc.android.base.utils.CommenUtil;
import com.dhcc.android.base.utils.Constants;
import com.dhcc.android.base.utils.FileUtil;
import com.dhcc.android.base.utils.version.UpdataInfo;

/**
 * 欢迎页面
 *
 * @author eric
 * @date 2014年8月28日
 */
public class WelcomeActivity extends SBaseActivity implements IWelcomeView {

    private Handler mHandler;// 用于实现延迟进入登陆页面
    public static String linkUrl;

    private String localVersion;
    private IWelcomePresenter mPresenter = new WelcomePresenter(this);


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setToolBarShow(false);
        setContentView(R.layout.main_welcome);
        SPreferencesManager.initContext(this);
    }

    @Override
    protected void setTitle(TextView toolbar_title) {

    }

    @Override
    protected void setToolBarNavigationIcon(Toolbar toolbar) {

    }

    @Override
    protected void initView() {
        initConfig();
        mPresenter.checkVersion();
    }

    @Override
    public void initConfig() {
        ImageView logo = (ImageView) findViewById(R.id.logo);
        Constants.screenHight = ((WindowManager) this
                .getSystemService(this.WINDOW_SERVICE)).getDefaultDisplay()
                .getHeight();
        Constants.screenWidth = ((WindowManager) this
                .getSystemService(this.WINDOW_SERVICE)).getDefaultDisplay()
                .getWidth();
        Constants.bottom_bar_Hight = Constants.screenHight * 19 / 240;
        RelativeLayout.LayoutParams layoutParam = new RelativeLayout.LayoutParams(
                Constants.screenHight / 6, Constants.screenHight / 6);
        layoutParam.setMargins(0, Constants.screenHight / 5, 0, 0);
        layoutParam.addRule(RelativeLayout.CENTER_HORIZONTAL);
        logo.setLayoutParams(layoutParam);
        //获取设备ID
        TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        Constants.DEVICE_ID = tm.getDeviceId();
        try {
            localVersion = getVersionName();
        } catch (Exception e) {
            Log.e("iniConfig", e.getMessage());
        }
    }

    @Override
    public void onCheckSuccess(UpdataInfo updataInfo) {
        /**
         * 跳转判断
         */
        Boolean isFirstLoginFlag = SPreferencesManager.getValue(MSApplication.FISRTLOGINSHOWFLAG, true);

        //加载 系统设置的参数
        String gps = SPreferencesManager.getValue(MSApplication.GPS_REFRESH_TIME, "5");
        String cdis = SPreferencesManager.getValue(MSApplication.CATCH_RANGE, "10");
        CommenUtil.changeSettings(MSApplication.GPS_REFRESH_TIME, gps);
        CommenUtil.changeSettings(MSApplication.CATCH_RANGE, cdis);

        if (isFirstLoginFlag) {
            isFirstLoginFlag = false;
            SPreferencesManager.getValue(MSApplication.FISRTLOGINSHOWFLAG,
                    isFirstLoginFlag);
            //初始设置
            SPreferencesManager.getValue(MSApplication.TASK_SAVE_PATH, FileUtil.getSavePath());
            SPreferencesManager.getValue(MSApplication.GPS_REFRESH_TIME, "5");
            SPreferencesManager.getValue(MSApplication.NOTE_DISTANCE, "2");
            SPreferencesManager.getValue(MSApplication.CATCH_RANGE, "10");
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        } else {// 不是首次进入
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        }

    }
}
